package com.itheima.controller;

import com.itheima.dao.StudentDao;
import com.itheima.domain.Student;
import com.itheima.service.StudentService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

@WebServlet("/addServlet")
public class AddServlet  extends HttpServlet {
    @Override

 protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        //1.接收参数
        String name = req.getParameter("name");
        String age1 = req.getParameter("age");
        int age = Integer.parseInt(age1);
        String birthday = req.getParameter("birthday");
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

        Date date = null;
        try {
            date = sdf.parse(birthday);

        } catch (ParseException e) {
            e.printStackTrace();
        }

        //2.封装对象
        Student student = new Student(null,name,age,date);

        //调用业务层，保存数据
        StudentService service = new StudentService();
        service.add(student);

        //跳转到index.jsp
        //req.getRequestDispatcher("/index.jsp").forward(req,resp);

       // resp.sendRedirect("/index.jsp");
       resp.sendRedirect("/showStudent");
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req,resp);
    }

}

